<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<script src="../vue.js"></script>
		
		<!-- 侦听器的例子需要用到js -->
		<!-- 因为 AJAX 库和通用工具的生态已经相当丰富，Vue 核心代码没有重复 -->
		<!-- 提供这些功能以保持精简。这也可以让你自由选择自己更熟悉的工具。 -->
		<script src="https://cdn.jsdelivr.net/npm/axios@0.12.0/dist/axios.min.js"></script>
		<script src="https://cdn.jsdelivr.net/npm/lodash@4.13.1/lodash.min.js"></script>
		
		<!-- https://cn.vuejs.org/v2/guide/computed.html -->
		<title>计算属性和侦听器</title>
	</head>
	<body>
		<div id="demo1">

			<p>Message is :{{message}}</p>
			<p>计算属性 reverseMessage is : {{reversedMessage}}</p>
			<!-- 计算属性缓存 vs 方法 -->
			<p>当前时间：{{now}}</p>
			<p>当前时间2：{{now}}</p>
		</div>
		<!-- 计算属性 vs 侦听属性 -->
		<div id="demo2">{{ fullName2 }}</div>

		<!-- 计算属性的 setter -->
		<div id="demo3">{{ fullName3 }}</div>
		
		<!-- 侦听器 -->
		<div id="watch-example">
		  <p>
		    Ask a yes/no question:
		    <input v-model="question">
		  </p>
		  <p>{{ answer }}</p>
		</div>
		
		<script>
			var data = {
				message: '计算属性和侦听器',
				message2: '',
				seen: true, // false
				url: 'https://cn.vuejs.org/v2/guide/list.html',
				isActive: true,
				isGreen: true,
				color: '#FF0000',
				size: '50px',
				type: 'C',
				isShow: true,
				numbers: [1, 2, 3, 4, 5, 6],
				counter: 0,
				checkedNames: [],
				picked: '',
				txt: 'test',
				firstName: 'Foo',
				lastName: 'Bar',
				fullName: 'Foo Bar',

			}

			var vm1 = new Vue({
				el: '#demo1',
				data: data,
				methods: {
					clicknow: function(e) {
						console.log(e)
					}
				},

				computed: {
					reversedMessage: function() {
						return this.message.split('').reverse().join('')
					},

					now: function() {
						// return new Date(Date.now()).toLocaleString()//2024/5/31 下午3:25:44
						return Date.now() //时间戳
					},
				},

				watch: {
					firstName: function(val) {
						this.fullName = val + ' ' + this.lastName
					},
					lastName: function(val) {
						this.fullName = this.firstName + ' ' + val
					}
				},
			})

			// 计算属性 vs 侦听属性
			var vm2 = new Vue({
				el: '#demo2',
				data: {
					firstName2: 'Foo',
					lastName2: 'Bar'
				},
				computed: {
					//watch里的代码是命令式且重复的。将它与计算属性的版本进行比较：
					fullName2: function() {
						return this.firstName2 + ' ' + this.lastName2
					},
				}
			})

			// 计算属性的 setter
			var vm3 = new Vue({
				el: '#demo3',
				data: {
					firstName3: 'Foo',
					lastName3: 'Bar',
					
				},
				computed: {
					//计算属性默认只有 getter，不过在需要时你也可以提供一个 setter：
					fullName3: {
						get: function(newName) {
							console.log('执行了get')
							return this.firstName3 + ' ' + this.lastName3
						},
						set: function(newValue) {
							console.log('执行了set')
							var names = newValue.split(' ')
							this.firstName3 = names[0]
							this.lastName3 = names[names.length - 1]
						}

					},
				}
			})
			vm3.fullName3 = 'John Doe'

			// vm1.message = vm1.message.split('').reverse().join('')

			// console.log(vm1.reversedMessage) // => '计算属性和侦听器'
			// vm1.message = 'Goodbye'
			// console.log(vm1.reversedMessage) // => 'eybdooG'
			
			
			
			// 侦听器
			var watchExampleVM = new Vue({
			  el: '#watch-example',
			  data: {
			    question: '',
			    answer: 'I cannot give you an answer until you ask a question!'
			  },
			  watch: {
			    // 如果 `question` 发生改变，这个函数就会运行
			    question: function (newQuestion, oldQuestion) {
			      this.answer = 'Waiting for you to stop typing...'
			      this.debouncedGetAnswer()
			    }
			  },
			  created: function () {
			    // `_.debounce` 是一个通过 Lodash 限制操作频率的函数。
			    // 在这个例子中，我们希望限制访问 yesno.wtf/api 的频率
			    // AJAX 请求直到用户输入完毕才会发出。想要了解更多关于
			    // `_.debounce` 函数 (及其近亲 `_.throttle`) 的知识，
			    // 请参考：https://lodash.com/docs#debounce
			    this.debouncedGetAnswer = _.debounce(this.getAnswer, 500)
			  },
			  methods: {
			    getAnswer: function () {
			      if (this.question.indexOf('?') === -1) {
			        this.answer = 'Questions usually contain a question mark. ;-)'
			        return
			      }
			      this.answer = 'Thinking...'
			      var vm = this
			      axios.get('https://yesno.wtf/api')
			        .then(function (response) {
			          vm.answer = _.capitalize(response.data.answer)
			        })
			        .catch(function (error) {
			          vm.answer = 'Error! Could not reach the API. ' + error
			        })
			    }
			  }
			})
			
		</script>
	</body>
</html>